﻿@{
    ViewData["Title"] = "文章列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" onclick="existAuth('add')"> 添加 </button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete" onclick="existAuth('deletes')"> 放入回收站 </button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-success easyadmin-export-btn" lay-event="import" >导入</button>
                <a href="/api/cms/artcile/Export"  class="layui-btn layui-btn-sm layui-btn-success easyadmin-export-btn" lay-event="export" >导出</a>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" onclick="existAuth('modify')">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" onclick="existAuth('delete')">真删除</a>
        </script>
    </div>
</div>

@section js{
<script type="text/html" id="switchAudit">
    <input type="checkbox" name="Audit" value="{{d.id}}"  lay-skin="switch" lay-filter="Audit"  lay-text="ON|OFF" {{ d.audit==1?'checked':''}}>
</script>
<script type="text/html" id="switchIstop">
    <input type="checkbox" name="Istop" value="{{d.id}}"  lay-skin="switch" lay-filter="istop"  lay-text="ON|OFF" {{ d.isTop==1?'checked':''}}>
</script>
<script type="text/html" id="switchIsComment">

    <input type="checkbox" name="IsComment" value="{{d.id}}"  lay-skin="switch" lay-filter="IsComment"  lay-text="ON|OFF" {{ d.isComment==1?'checked':''}}>
</script>
<script type="text/html" id="switchIsHot">
    <input type="checkbox" name="IsHot" value="{{d.id}}"  lay-skin="switch" lay-filter="IsHot"  lay-text="ON|OFF" {{ d.isHot==1?'checked':''}}>
</script>
<script>
    layui.use(['form', 'layer', 'table', 'element', 'common'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            apiUtil = layui.common,
            layer = layui.layer;
        //layui button onclick必须这样写，不然会出现事件未定义；
        window.existAuth = function (btnName) {
            /*
             第一个参数是列表名称和数据库nameCode一定要对应;
             第二个参数是按钮名称和数据库的btnName也要对应的;
             event 阻止事件冒泡；
            */
            apiUtil.checkAuth("article", btnName, event);
        };
        apiUtil.render({
            elem: '#currentTableId',
            url: 'cms/article/getListPages',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            authorizeFields: true, // 字段权限开关
            autoarticleWidth: true,
            overflow: { // 默认所有表格都超出
                type: 'tips'
                , hoverTime: 300 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                , color: 'black' // 字体颜色
                , bgColor: 'white' // 背景色
                , header: true, // 表头支持 overflow
                total: true // 合计行支持 overflow
            },
            cols: [[
                { type: "checkbox", width: 30 },
                { field: 'id', width: 60, title: 'Id', sort: true },
                { field: 'tenantName', title: '租户名称' , width: 100},
                { field: 'columnName', title: '栏目名称' , width: 100},
                {
                    field: 'title', title: '标题', width: 350,templet: function (res) {
                        return '<a href="javascript:void(0)" lay-event="edit" class="text-color">' + res.title + '</a>';
                    }
                },
                { field: 'author', title: '作者' , width: 90},
                { field: 'source', title: '来源' , width: 90},
                { field: 'audit', width: 90, title: '审状态', templet: '#switchAudit' },
                { field: 'isTop', width: 90, title: '是否置顶', templet: '#switchIstop' },
                { field: 'isComment', width: 90, title: '是否评论', templet: '#switchIsComment' },
                { field: 'isHot', width: 90, title: '是否热点', templet: '#switchIsHot' },
                { field: 'createTime', title: '创建时间' },
                { field: 'modifyTime', title: '修改时间' },
                { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
            ]],
            parseData: function (res) {
                return apiUtil.parseDataFun(res);
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
           // var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    key: data.field.title
                }
            }, 'data');
            return false;
        });

        function refeshTable() {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
            }, 'data');
        };

           form.on('switch(Audit)', function (data) {
              ChangeState(data,"Audit");
            });
            form.on('switch(IsComment)', function (data) {
              ChangeState(data,"IsComment");
            });
            form.on('switch(IsHot)', function (data) {
              ChangeState(data,"IsHot");
            });
            form.on('switch(istop)', function (data) {
              ChangeState(data,"IsTop");
            });

         function ChangeState(data,type){
            console.log("type:"+type);
         var state=data.elem.checked?true:false;        
               var id= data.value;
                apiUtil.ajax('cms/article/ChangeState', { id: id,stateValue: state,type:type}, "application/json", "post", function (res) {
                            if (res.success == true) {
                                layer.msg(res.msg);
                            } else {
                                layer.msg(res.msg);
                            }
                        });
        };
        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            if (obj.event === 'add') {
                var content = '/cms/article/modify';
            layer.open({
                    title: '添加文章',
                    type: 2,
                    area: ['100%', '100%'],
                    zIndex: "10000",
                    content: content,
                    end: function () {
                        refeshTable();
                    }
                });

            } else if (obj.event === 'delete') {  // 监听删除操作

                var listIds = [];
                if (data.length > 0) {
                    for (var i in data) {
                        listIds.push(data[i].id);
                    }
                    layer.confirm('您确定执行该操作吗？', { icon: 3, title: '提示信息' }, function (index) {
                        apiUtil.ajax('cms/article/softDelete', { ids: listIds }, "application/json", "delete", function (res) {
                            if (res.statusCode == 200 && res.success == true) {
                                layer.msg(res.msg);
                                layer.close(index);
                                refeshTable();
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    })
                } else {
                    layer.msg("请选择需要删除的数据");
                }
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var content = '/cms/article/modify?id=' + data.id;
                var index = layer.open({
                    title: '编辑文章',
                    // tips: [1, '#c00'],
                    type: 2,
                    shade: 0.2,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    zIndex: "10000",
                    content: content,
                    end: function () {
                        refeshTable();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('您确定删除吗', function (index) {
                    var listIds = new Array();
                    listIds[0] = data.id;
                    apiUtil.ajax('cms/article/deletes', { ids: listIds }, "application/json", "delete", function (res) {
                        if (res.statusCode == 200 && res.success == true) {
                            layer.msg(res.msg);
                            layer.close(index);
                            refeshTable();
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        });

    });
</script>
}
